-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New lint: unused_enumerate_value
#14443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
f99f935
to
5428c6b
Compare
Ideally we should also check if the arbitrary type implements impl ExactSizeIterator, the trait isn't very well known. |
Emm.. I doubt it because |
In that case, we could just emit a warning if it's not coming from a ExactSizeIterator. I don't think we'll get much use if we limit the lint to just the trait. |
79ce2bf
to
81c7daa
Compare
Updated. Now arbitrary type is covered. |
@blyxyas does this look good now? |
This comment has been minimized.
This comment has been minimized.
&& let Some((DefKind::AssocFn, call_id)) = cx.typeck_results().type_dependent_def(arg.hir_id) | ||
&& cx.tcx.is_diagnostic_item(sym::enumerate_method, call_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Smart here to check for both Enumerate
type and .enumerate()
. This would avoid us linting on cases like iter.enumerate().map()
, which could use iter in unforeseen ways.
&& let receiver_ty = cx.typeck_results().expr_ty(recv) | ||
// TODO: Replace with `sym` when it's available | ||
&& let Some(exact_size_iter) = get_trait_def_id(cx.tcx, &paths::ITER_EXACT_SIZE_ITERATOR) | ||
&& implements_trait(cx, receiver_ty, exact_size_iter, &[]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leaving this as an open question for the FCP, but I still think that we should not only lint for types that implement iter_exact_size_iterator
, as len()
being an idiomatic standard is practically guaranteed. In fact, len()
that does not report exact length as an usize
would be considered a very bad practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking for len
is implemented in remove_trailing_iter
because we should only remove the iter
call if the iterator is created in this expr.
Everything is looking great! I opened the FCP on Zulip |
From the FCP I find it not that reasonable as I initial thought about this lint. I'll close this pr for now. |
for (i, _) in iter.enumerate()
can be replaced withfor i in 0..iter.len()
if the iterator implementsExactSizeIterator
.Closes #14430
changelog: [
unused_enumerate_value
]: new lint